<?php

namespace app\partner\controller;

use app\common\controller\CommonService;
use think\Controller;
use think\Db;
use think\facade\Validate;
use think\Request;

class HomePage extends Controller
{

    /**
     * 欢迎页数据
     * @return \think\response\Json
     */
    public function welcomePage(Request $request)
    {
        $partnerId = $request->partnerId;
        for ($i = 1; $i <= 7; $i++) {
            $date = date('Y-m-d', strtotime('+' . $i - 7 . ' days', time()));
            $time = date('m-d', strtotime('+' . $i - 7 . ' days', time()));
            $data[$time] = $this->getData(strtotime($date), strtotime($date) + 86400, $partnerId);
        }
        foreach ($data as $k => $v){
            $data[$k]['orderSumSuperPartnerRebateFee'] = round($v['orderSumSuperPartnerRebateFee'],3);
            $data[$k]['orderSumUserBuyPrice'] = round($v['orderSumUserBuyPrice'],3);
        }
        return CommonService::success($data);
    }


    private function getData($startTime, $endTime, $partnerId = null)
    {
        if (!$partnerId) $partnerId = \request()->partnerId;
        $uids = Db::name('user_manage')->where('partner_id', $partnerId)->column('id');
        if (empty($uids)){
            $data['addUser'] = 0;
            $data['orderCount'] = 0;
            $data['orderSumUserBuyPrice'] = 0;
            $data['orderSumSuperPartnerRebateFee'] = 0;
            return $data;
        }
        $tbOrder = Db::name('tb_order')
            ->leftJoin('user_manage u','tb_order.uid = u.id')
            ->where('u.partner_id', $partnerId)
            ->where('order_type', 1)
            ->where('order_status', '<>', 0)
            ->where('is_weiquan', 0)
            ->whereBetween('order_time', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)])
            ->field("count(*) as orderCount,sum(user_buy_price) as orderSumUserBuyPrice,sum(super_partner_rebate_fee) as orderSumSuperPartnerRebateFee")
            ->find();
        $jdOrder = Db::name('jd_order')
            ->leftJoin('user_manage u','jd_order.uid = u.id')
            ->where('u.partner_id', $partnerId)
            ->where('order_status', '<>', 0)
            ->whereBetween('order_time', [$startTime * 1000, $endTime * 1000])
            ->field("count(*) as orderCount,sum(user_buy_price) as orderSumUserBuyPrice,sum(super_partner_rebate_fee) as orderSumSuperPartnerRebateFee")
            ->find();
        $pddOrder = Db::name('pdd_order')
            ->leftJoin('user_manage u','pdd_order.uid = u.id')
            ->where('u.partner_id', $partnerId)
            ->where('order_status', '<>', 0)
            ->whereBetween('order_time', [$startTime, $endTime])
            ->field("count(*) as orderCount,sum(user_buy_price) as orderSumUserBuyPrice,sum(super_partner_rebate_fee) as orderSumSuperPartnerRebateFee")
            ->find();
        $data['addUser'] = Db::name('user_manage')->where('partner_id', $partnerId)->whereBetween('time', [date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime)])->count();
        $data['orderCount'] = $tbOrder['orderCount'] + $jdOrder['orderCount'] + $pddOrder['orderCount'];
        $data['orderSumUserBuyPrice'] = $tbOrder['orderSumUserBuyPrice'] + $jdOrder['orderSumUserBuyPrice'] + $pddOrder['orderSumUserBuyPrice'];
        $data['orderSumSuperPartnerRebateFee'] = $tbOrder['orderSumSuperPartnerRebateFee'] + $jdOrder['orderSumSuperPartnerRebateFee'] + $pddOrder['orderSumSuperPartnerRebateFee'];
        return $data;
    }


    /**
     * 登录日志
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function loginLog(Request $request)
    {
        $partnerId = $request->partnerId;
        $data['data'] = Db::name('login_log')
            ->field('login_time,login_ip,login_area,login_browser')
            ->where('uid', $partnerId)
            ->where('user_type', 2)
            ->order('login_time','desc')
            ->page(input('page', 1), input('listRows', 20))
            ->select();
        $data['total'] = Db::name('login_log')
            ->where('uid', $partnerId)
            ->where('user_type', 2)
            ->count();
        return CommonService::success($data);
    }


    /**
     * 系统消息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function systemNotice(Request $request)
    {
        $partnerId = $request->partnerId;
        $data['data'] = Db::name('partner_system_notice')
            ->field('id,title,content,add_time,read_time')
            ->where('partner_id', $partnerId)
            ->where('partner_type', 2)
            ->page(input('page', 1), input('listRows', 20))
            ->select();
        $data['total'] = Db::name('partner_system_notice')
            ->where('partner_id', $partnerId)
            ->where('partner_type', 2)
            ->count();
        $data['unreadTotal'] = Db::name('partner_system_notice')
            ->where('partner_id', $partnerId)
            ->where('partner_type', 2)
            ->where('read_time', 0)
            ->count();
        return CommonService::success($data);
    }


    /**
     * 已读消息
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function readNotice(Request $request)
    {
        $partnerId = $request->partnerId;
        $requestData = $request->post();
        $validate = Validate::make([
            'id' => ['regex' => '^(\d{1,10}|all)$', 'require']
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }

        if ($requestData['id'] == 'all') {
            Db::name('partner_system_notice')
                ->where('partner_id', $partnerId)
                ->where('partner_type', 2)
                ->where('read_time', 0)
                ->update(['read_time' => time()]);
        } else {
            Db::name('partner_system_notice')
                ->where('partner_id', $partnerId)
                ->where('partner_type', 2)
                ->where('read_time', 0)
                ->where('id', $requestData['id'])
                ->update(['read_time' => time()]);
        }
        return CommonService::success([]);

    }
}
